// pages/myLike/myLike.js
import profile from '../../profile.js'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lang: app.globalData.language,
    index: profile.myLike,

    // tab 切换相关
    indexClassNav: profile.myLike.indexClassNav,
    navNum: 0,

    // 作品相关
    items: profile.myLike.classProList,
    masonryHeight: 0,

    // 艺术家相关
    artistList: profile.myLike.artistList
  },

  // 艺术家like点击事件
  artistLikeTap: function (e) {
    let id = e.target.id, list = this.data.artistList;
    for (let i=0;i<list.length;i++) {
      if (id == list[i].id) {
        if (list[i].isLike) {
          list[i].isLike = false;
        }else{
          list[i].isLike = true;
        }
      }
    }
    this.setData({
      artistList: list
    })
    profile.myLike.artistList = list;
  },

  // 单个作品like点击事件
  likeTap: function(e) {
    let id = e.detail.id, list = this.data.items;
    for (let i=0;i<list.length;i++){
        if (id == list[i].id) {
          if (list[i].like == true) {
            list[i].like = false;
            list[i].likeNum --;
            continue;
          }
          list[i].like = true;
          list[i].likeNum ++;
        }
    }
    this.setData({
      items: list
    })
    profile.myLike.classProList = list;
    this._doRefreshMasonry(this.data.items);
    this.masonryHeightFun(300);
  },

  // 课程tab切换事件
  changeClassNav: function (e) {
    let id = e.detail.id, list = this.data.indexClassNav;
    for (let i=0;i<list.length;i++){
      if (list[i].id == id) {
        list[i].onthis = true
      }else{
        list[i].onthis = false
      }
    }
    this.setData({
      indexClassNav: list,
      navNum: e.detail.id
    })
    if (id == 1) {
      this._doRefreshMasonry(this.data.items);
    }
    profile.myLike.indexClassNav = list;
  },

  // 语言切换事件
  menuTap: function (e) {
    this.setData({
      lang: e.detail.lang
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '我的收藏'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'My like'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this._doRefreshMasonry(this.data.items);
  },

  // 刷新瀑布流
  _doRefreshMasonry(items) {
    this.masonryListComponent = this.selectComponent('#masonry');
    this.masonryListComponent.start(items).then(() => {
      console.log('refresh completed')
    })
  },

  // 增加瀑布流内容
  _doAppendMasonry(items) {
    this.masonryListComponent = this.selectComponent('#masonry')
    // 获取接口数据后使用瀑布流组件append方法，当append完成后调用then，是否可触底价在的标志位可以在这里处理
    this.masonryListComponent.append(items).then(() => {
      console.log('refresh completed')
    })
  },

  // 瀑布流容器高度获取与设置
  masonryHeightFun: function (time) {
    let height = this.data.masonryHeight;
    setTimeout(() => {
      let query = wx.createSelectorQuery();
      query.select('#masonry').boundingClientRect(rect=>{
        height = rect.height;
        this.setData({
          masonryHeight: height
        })
      }).exec();
    }, time);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      lang: app.globalData.language
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '我的收藏'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'My like'
      })
    }
  },
})